
print("Hello, 初始化工具类 下载视频以及轮播图片")




def download_page(name, page_url):
    # pip install DrissionPage

    # 先配置环境 将chrome.exe设置到默认浏览器中
    # from DrissionPage import ChromiumOptions
    # path = r'C:\Program Files\Google\Chrome\Application\chrome.exe' # 自己电脑内chrom浏览器exe所在位置
    # ChromiumOptions().set_browser_path(path).save()

    import requests
    from DrissionPage import ChromiumPage

    import re
    import os
    from datetime import datetime

    # pip install pymysql
    import pymysql

    headers = {
        'referer': 'https://www.douyin.com/',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36'
    }
    dp = ChromiumPage()  # 自动打开chrome浏览器

    def clean_name(name):
        # 替换文件名中的特殊字符为下划线
        return re.sub(r'[<>:"/\\?*\s]', "_", name).rstrip(' .')[:20]

    name = clean_name(name)

    try:
        # 监听数据包 请求路径是：https://www.douyin.com/aweme/v1/web/aweme/post/？xxx
        dp.listen.start('aweme/v1/web/aweme/post/')

        """
        下载指定博主的主页
        :param name: 博主名称
        :param page_url: 博主主页URL
        """
        print(f'正在下载 {name} 的主页: {page_url}')

        dp.get(page_url)
        save_dir = r'D:\douyin\video' + '\\' + name
        # 检查文件夹是否存在，如果不存在则创建
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)

        def clean_filename(filename):
            # 替换文件名中的特殊字符为下划线
            return re.sub(r'[<>:"/\\?*\s]', "_", filename).rstrip(' .')[:20]

        # 使用当前时间生成 格式为2025-01-01_22_22_22的日期字符串
        def get_date_str():
            return datetime.now().strftime("%Y-%m-%d_%H_%M_%S")

        def video_id_exists(video_id):
            """
            检查video_id是否已经存在于数据库中
            :param video_id: 视频的ID
            :return: 如果存在返回True，否则返回False
            """
            conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', charset='utf8mb4', db='video')
            cursor = conn.cursor()
            sql = "SELECT * FROM art WHERE aweme_id = %s"
            cursor.execute(sql, [video_id])
            result = cursor.fetchone()
            cursor.close()
            conn.close()
            return result is not None

        def save_to_mysql(author_user_id, name, title, video_id, create_time):
            try:
                conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', charset='utf8mb4',
                                       db='video')
                cursor = conn.cursor()

                add_time = datetime.now()
                create_time = datetime.fromtimestamp(create_time)

                # 将create_time 从时间戳"1755280875"转成dateTime格式的时间格式存入数据库中对应的dateTime类型


                # 插入数据的SQL语句
                insert_query = "INSERT INTO art (author_user_id, user_name, title, aweme_id, create_time, add_time) VALUES (%s, %s, %s, %s, %s, %s)"
                cursor.execute(insert_query, (author_user_id, name, title, video_id, create_time, add_time))

                # 提交事务
                conn.commit()
                print(f'{name} {title} {video_id} 已保存到数据库')

            except conn.Error as err:
                print(f"插入数据时出错: {err}")

            finally:
                # 关闭游标和数据库连接
                if cursor:
                    cursor.close()
                if conn:
                    conn.close()

        # 页码
        page_i = 1

        # 构建翻页
        #for page in range(1, 2):
        while True:

            print("\r\n")
            print(f'正在采集{page_i}页的内容')

            try:
                resp = dp.listen.wait(timeout=6)

                json_data = resp.response.body
                ## print(json_data)
                info_list = json_data['aweme_list']
                print(f'当前页码 {page_i} 页 共 {len(info_list)} 条数据')
                # 这里是判断是否有数据--永远进不来，因为这里是卡住了监听数据包，没数据的时候，这里一直卡住进不来啊，得改方式判断到底了，退出循环
                # if info_list is None:
                #     # 没有数据
                #     print(f'没有数据了 当前页码 {page_i} 页，退出循环')
                #     break

                if not info_list:
                    print(f'没有数据了 当前页码 {page_i} 页，退出循环')
                    break

                for index in info_list:
                    title = index['desc']
                    video_id = index['aweme_id']
                    create_time = index['create_time']
                    author_user_id = index['author_user_id']

                    # 根据video_id去查mysql库，如果已经存在，则跳过
                    # 这里是查询数据库的操作
                    if video_id_exists(video_id):
                        print(f'{name} {title} {video_id} 已经存在')
                        continue
                    else:
                        # 保存入mysql库
                        print(f'{name} {title} {video_id} 开始下载')
                        save_to_mysql(author_user_id, name, title, video_id, create_time)

                    media_type = index['media_type']
                    if media_type == 4:
                        try:
                            # 这里是视频的地址，如果是相册的话，这里是个mp3
                            video_url = index['video']['play_addr']['url_list'][0]
                            # print(title, video_url)
                            # 清理文件名
                            clean_title = clean_filename(title)
                            print(name, clean_title, video_url)

                            video_content = requests.get(url=video_url, headers=headers).content
                            with open(f'{save_dir}\\' + clean_title + '-' + video_id + '.mp4', 'wb') as f:
                                f.write(video_content)
                        except Exception as e:
                            print('没有视频')
                            print(e)
                            continue
                    if media_type == 2:
                        try:
                            # 清理文件名
                            clean_title = clean_filename(title)
                            # 这里是图片的地址
                            image_list = index['images']
                            num = 0
                            # 生成 2025-01-01-22-01-09-001 格式的图片名称
                            dateStr = get_date_str()
                            for index_pic in image_list:
                                num = num + 1
                                name_index = f"{name}-{dateStr}-{num:03d}"
                                image_url = index_pic['url_list'][0]
                                print(f'图片地址 {name} {image_url}')
                                # 图片地址 https://p3-pc-sign.douyinpic.com/tos-cn-i-0813/ogIAyk8AHCXAPg9JaJQAGeDA9fnBY2AFKbux3N~tplv-dy-aweme-images:q75.webp?lk3s=138a59ce&x-expires=1749222000&x-signature=gi%2FXNiSpR7VW09eVKJikoXPYfM4%3D&from=327834062&s=PackSourceEnum_PUBLISH&se=false&sc=image&biz_tag=aweme_images&l=202505072307365885563EA8DBBB19265C
                                picResp = requests.get(url=image_url, headers=headers)
                                with open(f'{save_dir}\\' + clean_title + '-' + name_index + '.webp', 'wb') as f:
                                    f.write(picResp.content)
                        except Exception as e:
                            print('没有图片')
                            print(e)
                            continue
                    if media_type == 42:
                        try:
                            # 清理文件名
                            clean_title = clean_filename(title)
                            # 这里是图片的地址
                            image_list = index['images']
                            num = 0
                            # 生成 2025-01-01-22-01-09-001 格式的图片名称
                            dateStr = get_date_str()
                            for index_pic in image_list:
                                num = num + 1
                                name_index = f"{name}-{dateStr}-{num:03d}"
                                image_url = index_pic['url_list'][0]
                                print(f'静态图地址 {name} {image_url}')
                                picResp = requests.get(url=image_url, headers=headers)
                                with open(f'{save_dir}\\' + clean_title + '-' + name_index + '.webp', 'wb') as f:
                                    f.write(picResp.content)

                            for index_pic in image_list:
                                num = num + 1
                                name_index = f"{name}-{dateStr}-{num:03d}"
                                video_image_url = index_pic['video']['play_addr']['url_list'][0]
                                print(f'动图地址 {name} {video_image_url}')
                                # 动图地址 https://v3-web.douyinvod.com/9309d42f20de02108111908a29352f0d/69272ffc/video/tos/cn/tos-cn-ve-15c000-ce/oA8DyfQQIAW2DjuIGSkEAeF4iTIFQlephRf25y/?a=6383&ch=10010&cr=3&dr=0&lr=all&cd=0%7C0%7C0%7C3&br=582&bt=582&cs=0&ds=3&ft=AJkeU_TERR0si0C4kDn2Nc0iPMgzbL9vwH-U_49GnU2JNv7TGW&mime_type=video_mp4&qs=0&rc=O2gzNTw1NWY0OTk1aDY5aUBpajs3b2w5cnk3NDMzbGkzNEBfLzYuM2JjX18xXmFhLS9hYSNwNTJhMmRrNDFhLS1kLWJzcw%3D%3D&btag=c0000e00008000&cquery=100o&dy_q=1764172266&feature_id=fea919893f650a8c49286568590446ef&l=20251126235105366ADDC2F6E32A288E2C
                                picResp = requests.get(url=video_image_url, headers=headers)
                                with open(f'{save_dir}\\' + clean_title + '-' + name_index + '.mp4', 'wb') as f:
                                    f.write(picResp.content)
                        except Exception as e:
                            print('没有动图')
                            print(e)
                            continue

                # 定位底部元素标签
                down = dp.ele('css:.Rcc71LyU')
                # 滚动操作
                dp.scroll.to_see(down)

                page_i = page_i + 1

            except Exception as e:
                print(e)
                print(f'超时了，没有收到数据 当前页码 {page_i} 页，退出循环')
                dp.quit()
                break
    except Exception  as e:
        print(f'发生异常: {e}')
    finally:
        # 确保浏览器关闭
        dp.quit()
        print("异常处理，浏览器已关闭。")










